import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutterhello/api/api.dart';
import 'package:flutterhello/controller/Controller.dart';
import 'package:flutterhello/util/host_util.dart';
import 'package:flutterhello/widgets/my_toast.dart';
import 'package:flutterhello/widgets/tips_card.dart';
import 'package:get/get.dart' as MyGet;

class JoinActPage extends StatefulWidget {
  @override
  _JoinActPageState createState() => _JoinActPageState();
}

class _JoinActPageState extends State<JoinActPage> {
  int type = 1;
  var choseTeam = null;
  var info;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.info = MyGet.Get.arguments;
  }

  @override
  Widget build(BuildContext _context) {
    final controller = MyGet.Get.put(Controller());
    return Scaffold(
      appBar: AppBar(
        title: Text('参与活动'),
        centerTitle: true,
      ),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          children: [
            Container(
              height: 200,
              width: double.infinity,
              child: TextFormField(
                cursorColor: Colors.black87,
                cursorWidth: 0.5,
                maxLines: 7,
                style: TextStyle(color: Colors.black54, fontSize: 16),
                scrollPadding: EdgeInsets.all(0),
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(0),
                    hintText: '输入留言',
                    hintStyle: TextStyle(fontSize: 14, color: Colors.black26),
                    border: InputBorder.none),
                onChanged: (val) {},
              ),
            ),
            Container(
              width: double.infinity,
              height: 50,
              decoration: BoxDecoration(
                border: Border(
                    top: BorderSide(color: Colors.black26),
                    bottom: BorderSide(color: Colors.black26)),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text('参与形式:'),
                  SizedBox(
                    width: 20,
                  ),
                  Row(
                    children: [
                      Text("团队"),
                      Radio(
                        value: 1,
                        focusColor: Colors.blueGrey,
                        activeColor: Colors.blueGrey,
                        groupValue: this.type,
                        onChanged: (value) {
                          setState(() {
                            this.type = value;
                          });
                        },
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Text("个人"),
                      Radio(
                        value: 2,
                        groupValue: this.type,
                        focusColor: Colors.blueGrey,
                        activeColor: Colors.blueGrey,
                        onChanged: (value) {
                          setState(() {
                            this.type = value;
                          });
                        },
                      ),
                    ],
                  )
                ],
              ),
            ),
            SizedBox(
              height: 10,
            ),
            this.type == 1
                ? GestureDetector(
                    child: this.choseTeam == null
                        ? Container(
                            width: double.infinity,
                            height: 50,
                            decoration: BoxDecoration(
                              border: Border(
                                  top: BorderSide(color: Colors.black26),
                                  bottom: BorderSide(color: Colors.black26)),
                            ),
                            child: Row(
                              children: [
                                Text(
                                  '选择团队',
                                  style: TextStyle(color: Colors.black45),
                                ),
                              ],
                            ),
                          )
                        : TipsCard(
                            favicon:
                                HostUtil.getHost() + this.choseTeam['favicon'],
                            name: this.choseTeam['groupname'],
                          ),
                    onTap: () {
                      showModalBottomSheet<void>(
                        context: context,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(20),
                            topRight: Radius.circular(20),
                          ),
                        ),
                        builder: (BuildContext context) {
                          return EasyRefresh.custom(
                            slivers: <Widget>[
                              SliverToBoxAdapter(
                                child: SizedBox(
                                  height: 20,
                                ),
                              ),
                              SliverList(
                                delegate: SliverChildBuilderDelegate(
                                  (context, index) {
                                    return Padding(
                                      padding: EdgeInsets.only(
                                        left: 20,
                                        right: 20,
                                      ),
                                      child: Column(
                                        children: [
                                          SizedBox(
                                            height: 10,
                                          ),
                                          GestureDetector(
                                            child: TipsCard(
                                              favicon: HostUtil.getHost() +
                                                  controller
                                                          .myCreateGroup[index]
                                                      ['favicon'],
                                              name: controller
                                                      .myCreateGroup[index]
                                                  ['groupname'],
                                            ),
                                            onTap: () {
                                              this.choseTeam = controller
                                                  .myCreateGroup[index];
                                              Navigator.of(context).pop();
                                              setState(() {});
                                            },
                                          ),
                                        ],
                                      ),
                                    );
                                  },
                                  childCount: controller.myCreateGroup.length,
                                ),
                              )
                            ],
                          );
                        },
                      );
                    },
                  )
                : Container(),
            SizedBox(
              height: 10,
            ),
            GestureDetector(
              child: Container(
                height: 50,
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.blueGrey,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                ),
                child: Center(
                  child: Text(
                    '提 交',
                    style: TextStyle(color: Colors.white, fontSize: 20),
                  ),
                ),
              ),
              onTap: () async {
                var result = await showDialog(
                    context: _context,
                    builder: (context) {
                      return AlertDialog(
                        title: Text('提示'),
                        content: Text('确认参与吗？'),
                        actions: <Widget>[
                          TextButton(
                            child: Text('取消'),
                            onPressed: () {
                              Navigator.of(context).pop('cancel');
                            },
                          ),
                          TextButton(
                            child: Text('确认'),
                            onPressed: () {
                              if (type == 1) {
                                // 团队
                                if (choseTeam != null) {
                                  submit(
                                      choseTeam['id'],
                                      choseTeam['favicon'],
                                      choseTeam['groupname'],
                                      info['userID'],
                                      'group',
                                      info['id'],
                                      _context);
                                } else {
                                  MyTost.showTost('请选择团队');
                                }
                              } else {
                                // 个人
                                submit(
                                    controller.userInfo['id'],
                                    controller.userInfo['favicon'],
                                    controller.userInfo['username'],
                                    info['userID'],
                                    'user',
                                    info['id'],
                                    _context);
                              }
                            },
                          ),
                        ],
                      );
                    });
                print('$result');
              },
            )
          ],
        ),
      ),
    );
  }
}

submit(
    fromID, fromFavicon, fromName, toUserID, type, activiteID, context) async {
  var res = await Api.applyJoinActviite({
    "fromID": fromID,
    "fromFavicon": fromFavicon,
    "fromName": fromName,
    "toUserID": toUserID,
    "type": type,
    "activiteID": activiteID,
  });
  if (res['code'] == 1000) {
    Navigator.of(context).pop();
    MyTost.showTost(res['message']);
    MyGet.Get.back();
  } else {
    Navigator.of(context).pop();
    MyTost.showTost(res['message']);
  }
}
